﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using Cashier_Utils;
using System.Data.Common;

namespace Cashier_Service
{
    public class MemberRechargeService
    {
        /// <summary>
        /// 查询充值信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="transno"></param>
        /// <returns></returns>
        public MemberRecharge GetMemberRecharge(long id, String transno)
        {
            MemberRecharge memberRecharge = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    memberRecharge = new MemberRecharge();
                    memberRecharge.Id = id;
                    memberRecharge.TransNo = transno;
                    memberRecharge = new MemberRechargeDAO().GetMemberRecharge(conn, memberRecharge);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return memberRecharge;
        }

        /// <summary>
        /// 查询充值记录信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="memberno"></param>
        /// <param name="transno"></param>
        /// <param name="name"></param>
        /// <param name="adminUsername"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetMemberRechargeTable(int pageindex, int pagesize, String enterpriseNo, String memberno, String transno, String name, 
            String adminUsername, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    MemberRecharge memberRecharge = new MemberRecharge();
                    memberRecharge.Pageindex = pageindex;
                    memberRecharge.Pagesize = pagesize;
                    memberRecharge.EnterpriseNo = enterpriseNo;
                    memberRecharge.MemberNo = memberno;
                    memberRecharge.TransNo = transno;
                    memberRecharge.Name = name;
                    memberRecharge.AdminUsername = adminUsername;
                    memberRecharge.Ascfield = ascfield;
                    memberRecharge.Descfield = descfield;
                    dt = new MemberRechargeDAO().GetMemberRechargeTable(conn, memberRecharge);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询充值信息记录数
        /// </summary>
        /// <param name="enterpriseNo"></param>
        /// <param name="memberno"></param>
        /// <param name="transno"></param>
        /// <param name="name"></param>
        /// <param name="adminUsername"></param>
        /// <returns></returns>
        public long GetMemberRechargeRecord(String enterpriseNo, String memberno, String transno, String name, String adminUsername)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    MemberRecharge memberRecharge = new MemberRecharge();
                    memberRecharge.EnterpriseNo = enterpriseNo;
                    memberRecharge.MemberNo = memberno;
                    memberRecharge.TransNo = transno;
                    memberRecharge.Name = name;
                    memberRecharge.AdminUsername = adminUsername;
                    record = new MemberRechargeDAO().GetMemberRechargeRecord(conn, memberRecharge);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入充值记录
        /// </summary>
        /// <param name="memberRecharge"></param>
        /// <returns></returns>
        public long InsertMemberRecharge(MemberRecharge memberRecharge)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new MemberRechargeDAO().InsertMemberRecharge(conn, memberRecharge);
                    if (id > 0)
                    {
                        //更新用户卡内余额
                        Decimal balance = memberRecharge.Money + memberRecharge.GiftMoney;
                        new MemberService().UpdateMemberBalance(conn, memberRecharge.MemberNo, balance, BalanceEnum.IN);
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 插入充值记录
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="memberRecharge"></param>
        /// <returns></returns>
        public long InsertMemberRecharge(DbConnection conn, MemberRecharge memberRecharge)
        {
            long id = 0;
            try
            {
                id = new MemberRechargeDAO().InsertMemberRecharge(conn, memberRecharge);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新充值信息
        /// </summary>
        /// <param name="memberRecharge"></param>
        /// <returns></returns>
        public int UpdateMemberRecharge(MemberRecharge memberRecharge)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new MemberRechargeDAO().UpdateMemberRecharge(conn, memberRecharge);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除充值记录信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteMemberRecharge(String ids)
        {
            int count = 0;
            try
            {
                if (!String.IsNullOrEmpty(ids))
                {
                    using (DbConnection conn = ConnectionFactory.GetDbConnection())
                    {
                        conn.Open();
                        MemberRechargeDAO memberRechargeDAO = new MemberRechargeDAO();
                        MemberRecharge memberRecharge = new MemberRecharge();
                        String[] idArr = ids.Split(new Char[] { ',' });
                        foreach (String lopid in idArr)
                        {
                            memberRecharge.Id = Int64.Parse(lopid);
                            count += memberRechargeDAO.DeleteMemberRecharge(conn, memberRecharge);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
